package com.net.oil.user;

import android.os.Bundle;
import
androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.http.AppUrl;
import com.net.oil.user.adapter.BusinessListAdapter;
import com.net.oil.user.bean.OilApplyBusinessProductRecordDetailVO;
import com.net.oil.user.contract.MyReceiveGoodsDetailContract;
import com.net.oil.user.presenter.MyReceiveGoodsDetailPresenter;
import com.net.oil.util.DateFormat;
import com.net.oil.util.glide.GlideUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyReceiveGoodsDetailActivity extends BaseActivity<MyReceiveGoodsDetailPresenter> implements MyReceiveGoodsDetailContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.goods_img)
    ImageView goodsImg;
    @BindView(R.id.goods_title_tv)
    TextView goodsTitleTv;
    @BindView(R.id.take_time_tv)
    TextView takeTimeTv;
    @BindView(R.id.status_tv)
    TextView statusTv;
    @BindView(R.id.end_time_tv)
    TextView endTimeTv;
    @BindView(R.id.code_tv)
    TextView codeTv;
    @BindView(R.id.info_title_tv)
    TextView infoTitleTv;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.show_info_layout)
    LinearLayout showInfoLayout;
    @BindView(R.id.code_layout)
    LinearLayout codeLayout;
    private int id = -1;

    private OilApplyBusinessProductRecordDetailVO productRecordDetail;

    private BusinessListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_receive_goods_detail);
        ButterKnife.bind(this);
        presenter = new MyReceiveGoodsDetailPresenter(this);
        initView();
        initData();
    }

    private void initData() {

        id = getIntent().getIntExtra(Constant.INTENT_MY_RECEIVE_GOODS_ID_KEY, -1);
        if (id != -1) {
            presenter.getMyReceiveGoodsDetail(id);
        }

    }

    private void initView() {
        toolbarTitle.setText("领取商品详情");

        adapter = new BusinessListAdapter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);

    }

    @OnClick({R.id.toolbar_go_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_go_back:
                finish();
                break;
        }
    }

    private void bindDataToView() {
        if (productRecordDetail != null) {
            if (!TextUtils.isEmpty(productRecordDetail.getProductName())) {
                goodsTitleTv.setText(productRecordDetail.getProductName());
            }

            String takeTime = DateFormat.formatDateTime(productRecordDetail.getReceiveTime(), DateFormat.FormatFlag.YDM);
            takeTimeTv.setText(takeTime);

            String endTime = DateFormat.formatDateTime(productRecordDetail.getExpireTime(), DateFormat.FormatFlag.YDM);
            endTimeTv.setText(endTime);

            GlideUtil.getInstance().loadImage(this, goodsImg, AppUrl.getImageUrl(productRecordDetail.getProductImage()), R.drawable.product_err);

            if (!TextUtils.isEmpty(productRecordDetail.getCode())) {
                codeTv.setText(productRecordDetail.getCode());
            }

            if (productRecordDetail.getUsed()) {
                statusTv.setText("已使用");
                updateList(true);
            } else {
                if (productRecordDetail.getExpireStatus() == 1) {
                    statusTv.setText("已过期");
                    codeLayout.setVisibility(View.GONE);
                } else {
                    statusTv.setText("未使用");
                    updateList(false);
                }
            }

        }
    }

    /**
     * 更新使用商家列表
     *
     * @param isUse 是否使用（未使用，则为可用商家列表，已使用，则为已使用的商家）
     */
    private void updateList(boolean isUse) {
        if (isUse) {
            if (productRecordDetail.getUsedBusinessInfo() != null) {
                showInfoLayout.setVisibility(View.VISIBLE);
                infoTitleTv.setText("使用门店");
                adapter.updateDatas(productRecordDetail.getUsedBusinessInfo());
            }
        } else {
            if (productRecordDetail.getSuitableForUseBusinessList() != null) {
                showInfoLayout.setVisibility(View.VISIBLE);
                infoTitleTv.setText("适用门店");
                adapter.updateDatas(productRecordDetail.getSuitableForUseBusinessList());
            }
        }
    }

    /**
     * 获取 我获取的商品详情成功
     *
     * @param myReceiveGoodsDetailBean 商品详情
     */
    @Override
    public void getMyReceiveGoodsDetailSuccess(OilApplyBusinessProductRecordDetailVO myReceiveGoodsDetailBean) {
        dismissProgressDialog();
        if (myReceiveGoodsDetailBean != null) {
            this.productRecordDetail = myReceiveGoodsDetailBean;
        }
        bindDataToView();
    }

    /**
     * 获取 我获取的商品详情失败
     */
    @Override
    public void getMyReceiveGoodsDetailFailed() {
        dismissProgressDialog();
    }


}
